

def merge():
    #unames=['user_id','gender','age','occupation','zip']
    #users=pd.read_table('ml-1m/users.dat',sep='::',header=None,names=unames)

    mnames = ['movie_id', 'title', 'genres']
    movies = pd.read_table('ml-1m/movies.dat', sep='::', header=None, names=mnames)

    rnames = ['user_id', 'movie_id', 'rating', 'timestamp']
    ratings = pd.read_table('ml-1m/ratings.dat', sep='::', header=None, names=rnames)

    all_data = pd.merge(pd.merge(ratings, users), movies)
    data = DataFrame(data=all_data,columns=['user_id','movie_id'])
    data.to_csv('data.csv')

merge()

data=pd.read_csv('data.csv')
X=data['user_id']
Y=data['movie_id']

item_user=dict()
for i in range(X.count()):
        user=X.iloc[i]
        item=Y.iloc[i]        
        if item not in item_user:
            item_user[item]=set()
        item_user[item].add(user)

C={}
N={}
for i,users in item_user.items():
        for u in users:
            N.setdefault(u,0)
            N[u]+=1
            C.setdefault(u,{})
            for v in users:
                if u==v:
                    continue
                C[u].setdefault(v,0)
                C[u][v]+=1




W=C.copy()
for u,related_users in C.items():
       for v,cuv in related_users.items():
            W[u][v]=cuv/math.sqrt(N[u]*N[v])


def recommend(user,user_item,W,K):
    rank={}
    interacted_items=user_item[user]
    for v,wuv in sorted(W[user].items(),reverse=True)[0:K]:
        for i in user_item[v]:
            if i not in interacted_items:
                rank.setdefault(i,0)
                rank[i]+=wuv
    return rank


